<?php
/**
 * 获取工单绑定延期原因的详情
 * User: Administrator
 * Date: 2018/8/5/005
 * Time: 20:38
 */

namespace app\lib\Order\OrderExtensionConfig;


use app\lib\Order\IsOrder\IsOrder;
use app\model\OrderBindingExtensionModel;

class GetOrderBindingExtension
{
    protected   $orderId;
    protected   $model;
    public function __construct($orderId)
    {
        $this->orderId = $orderId;
        if(!((new IsOrder($this->orderId))->isExecuteOrder())){
            exception('该工单状态不符合查询条件');
        }
    }

    protected function getModel(){
        if(empty($this->model)){
            $this->model = new OrderBindingExtensionModel();
        }
        return $this->model;
    }

    /**
     * 判断工单是否绑定延期标签
     * 绑定为true
     * @return bool
     */
    public function isOrderBindingExtension(){
        return $this->isExit($map = []);
    }

    /**
     * 判断工单是否绑定了待付款标签
     * @return bool
     */
    public function isOrderBindingPay(){
        $model =$this->getModel();
        $map[$model->_configId] = ['eq',2];
        return $this->isExit($map);
    }
    /**
     * 判断工单是否绑定了待签收标签
     * @return bool
     */
    public function isOrderBindingSign(){
        $model =$this->getModel();
        $map[$model->_configId] = ['eq',1];
        return $this->isExit($map);
    }
    /**
     * 判断工单是否绑定了普通标签
     * @return bool
     */
    public function isOrderBindingDaily(){
        $model =$this->getModel();
        $map[$model->_configId] = ['not in',[1,2]];
        return $this->isExit($map);
    }

    private function isExit($map){
        $model =$this->getModel();
        $map[$model->_orderId] =  $this->orderId;
        $map[$model->_status] = 1 ;
        $result =  $model->where($map)->value($model->_id);
        return  empty($result)?false:true;
    }

    /**
     * 获取除待签收，待付款标签以外标签的结束时间
     * @return mixed
     */
    public function getEndTime(){
        $model =  $this->getModel();
        $map   = [$model->_configId=>['neq',[1,2]]];
        return $this->getValueField($model->_endTime,$map);
    }


    protected function getValueField($field,$map){
        $model =  $this->getModel();
        $map[$model->_orderId]=$this->orderId;
        $map[$model->_status] = 1;
        return  $model->where($map)->value($field);
    }
}